import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Link } from 'react-router'
import { Button, Spin, Form, Input, Table, Card, Col, Row, DatePicker, Modal,message  } from 'antd'
import moment from 'moment';
/* 引用接口文档路径 */
import {
  fetchNewCheckList,addNews,deleteNews
} from 'actions/industry'
import '../../style/userControl.less'
import ShowBigImg from '../../components/showBigImg'
import ModalAddIndustryNews from './addIndustryNewsModal.js';
const { MonthPicker, RangePicker } = DatePicker;
const FormItem = Form.Item
@Form.create({})
//abc
@connect(
  (state, props) => ({
    config: state.config,
    userInfoList: state.userInfoList,
  })
)
export default class app extends Component {
  constructor(props) {
    super(props)
    this.state = {
      list:"",//数据列表
      addModalFlg:false,//新增要闻弹窗
      modalList:"",
      addModalType:"",
      showBigImgModal:false,//查看大图的弹窗
      imgArry:[],//图片数组
      imgIndex:"",//图片下标
    }
    this.currentNumber=1//当前页面
    this.keyword={"title":"","source":""};//搜索关键词
    this.pagesize = 10;
  }
  componentDidMount() {//页面首次加载
    this.selectList();
  }
  searchFN(){//点击搜索按钮
    this.keyword = {"title":this.props.form.getFieldValue('title'),"source":this.props.form.getFieldValue('source')};
    this.currentNumber=1;
    this.selectList();
  }
  selectList(pageNumber){//查询列表
    const datajson = Object.assign({"start":pageNumber||1,"end":this.pagesize}, this.keyword);
    this.currentNumber = pageNumber||1;
    this.props.dispatch(fetchNewCheckList(
      datajson,
      (respose) => {
        this.setState({
          list: respose
        })
    }))
  }

  addReport(type,data){//新增视频
    //编辑的情况  带数据去组件
    this.setState({
      addModalFlg: true,
      modalList:data,
      addModalType:type,
    })
  }
  addModalCancel(){
    this.setState({
      addModalFlg: false,
    });
  }

  deleteList(id){
    Modal.confirm({
      content:"确定要删除该条信息么？",
      onOk:()=>{
        this.props.dispatch(deleteNews(
          {"contentId":id},
          (respose) => {
            var length = this.state.list.respData.count ? this.state.list.respData.count : 0;
            if(Math.ceil(length/this.pagesize) == this.currentNumber && length%this.pagesize == 1 && this.currentNumber != 1){
              this.currentNumber = this.currentNumber - 1;
            }
            message.success(respose.respMsg);
            this.selectList(this.currentNumber);
        }))
      },
      onCancel:()=>{

      }
    })
  }
  
  showBigImg(item,index){//点击查看大图
    //item 图片数组 index 点击的下标
    //单张图片
    var arry = [item];
    this.setState({
      showBigImgModal:true,
      imgArry:arry,
      imgIndex:index
    })
  }
  showBigImgModalCancel(){//关闭查看大图弹窗
    this.setState({
      showBigImgModal:false,
      imgArry:[],
      imgIndex:''
    })
  }
  
  columns() {//表格标题
    return [
      {
        title: '要闻编号',
        key: 'code',
        dataIndex: 'code',        
        width: '15%',
      },
      {
        title: '要闻封面',
        dataIndex: 'imageAddr',
        key: 'imageAddr',
        width: '25%',
        render: (text, recordId, index) => <img 
          style={{width:"50px",cursor:"pointer"}} 
          title="点击查看大图" 
          onClick={this.showBigImg.bind(this,text ? text :  require('../../images/baseImg.png'),0)} 
          src={text ? text : require('../../images/baseImg.png')} 
        />,
      },
      {
        title: '要闻标题',
        dataIndex: 'title',
        key: 'title',
        width: '10%',
      },
      {
        title: '要闻来源',
        dataIndex: 'source',
        key: 'source',
        width: '10%',
      },
      {
        title: '添加时间',
        dataIndex: 'createTime',
        key: 'createTime',
        width: '15%',
      },
      {
        title: '阅读量',
        dataIndex: 'readTotal',
        key: 'readTotal',
        width: '15%',
      },
      {
        title: '操作',
        width: '20%',
        render: (text, recordId, index) => 
        <div>
          <Button type="primary" size="small" onClick={this.addReport.bind(this,2,text)} style={{marginRight:"5px",marginBottom:"5px"}}>编辑</Button>          
          <Button type="primary" size="small" onClick={this.deleteList.bind(this,text.id)} style={{marginRight:"5px",marginBottom:"5px"}}>删除</Button>  
        </div>,
      }
    ]
  }
  

  
  render () {
    const {form } = this.props
    const { getFieldDecorator } = form    
    const accountList = this.state.list.respData?this.state.list.respData:[];//刷新页面调用接口数据赋值
    const dateFormat = 'YYYY/MM/DD';
    return (
      <div className="page">
        {/* 搜索条件筛选块 */}
        <div className="searcDiv">
          <Form layout="inline">
            <FormItem label="要闻标题">      
              {
                getFieldDecorator('title', {
                  rules: [{
                    required: false,
                  }],
                })(
                  <Input placeholder="请输入关键字" size="default" style={{ width: '200px' }} />
                )
              }             
            </FormItem>
            <FormItem label="要闻来源">
            {
              getFieldDecorator('source', {
                rules: [{
                  required: false,
                }],
              })(
                <Input placeholder="请输入关键字" size="default" style={{ width: '200px' }} />
              )
            }                
            </FormItem>
            <Button  type="primary" onClick={this.searchFN.bind(this)}>搜索</Button>
            <Button  type="primary" onClick={this.addReport.bind(this,1,{})}>新增</Button>
          </Form>
        </div>
        {/*数据渲染列表*/}
        <Spin spinning={false}>
          <Table
            rowKey="id" 
            columns={this.columns()}//表格标题列
            dataSource={accountList.resultData}
            pagination={{ total: accountList.count, current:this.currentNumber, pageSize: this.pagesize , onChange: this.selectList.bind(this)}}
            scroll={{ y: true }}
          />
        </Spin>
        {/* 新增要闻弹窗 */}
        {
          this.state.addModalFlg?
          <ModalAddIndustryNews 
            type={this.state.addModalType}
            ModalFlg={this.state.addModalFlg} 
            ModalCancel={this.addModalCancel.bind(this)} 
            callback={this.selectList.bind(this)}
            content={this.state.modalList.id ? this.state.modalList : {}}
          /> : ""
        }
        {/* 查看大图的弹窗 */}
        {
          this.state.showBigImgModal?
          <ShowBigImg visible={this.state.showBigImgModal} imgArry={this.state.imgArry} imgIndex={this.state.imgIndex} closeModal={this.showBigImgModalCancel.bind(this)} />
          :""
        }
      </div>
    )
  }
}